package com.ellricky.newtv

import android.os.Bundle
import android.view.View
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.newtv.adapter.ImageAdapter
import com.ellricky.newtv.const.TvConst
import kotlinx.android.synthetic.main.fragment_image_preview.*

class NewTvImagePreviewFragment : BaseFragment<Void, BasePresenter<Void>>() {

    private val imageList: ArrayList<String> by lazy {
        arguments?.getStringArrayList(TvConst.KEY_IMAGE) ?: arrayListOf()
    }

    private val imageAdapter: ImageAdapter by lazy {
        ImageAdapter()
    }

    companion object {
        fun newInstance(imageList: ArrayList<String>): NewTvImagePreviewFragment {
            val args = Bundle()
            args.putStringArrayList(TvConst.KEY_IMAGE, imageList)
            val fragment = NewTvImagePreviewFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.fragment_image_preview
    }

    fun toLeft() {
        val currentItem = vp.currentItem
        if (currentItem - 1 < 0) {
            return
        }
        vp.setCurrentItem(currentItem - 1, true)
    }

    fun toRight() {
        val currentItem = vp.currentItem
        if (currentItem + 1 >= imageList.size) {
            return
        }
        vp.setCurrentItem(currentItem + 1, true)
    }

    override fun initUIView(p0: View?) {
        vp.adapter = imageAdapter
        imageAdapter.setImages(imageList)
    }
}